//
//  BQAccountManager.m
//  BeeQuick
//
//  Created by Asun on 2017/7/9.
//  Copyright © 2017年 BecomeRich. All rights reserved.
//

#import "BQAccountManager.h"
#import "BQCategoryModel.h"
#import "BQProductModel.h"

@implementation BQAccountManager

+ (BQAccountManager *)sharedInstance{
    
    static BQAccountManager *__instance__;
    
    static dispatch_once_t onceToken;
    
    dispatch_once(&onceToken, ^{
        __instance__ = [BQAccountManager new];
        
        __instance__.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/html", nil];
        
        __instance__.requestSerializer = [AFJSONRequestSerializer serializer];
        
    });
    return  __instance__;
    
}


- (void)POSTWithUrlString:(NSString *)urlString parameters:(id)parameters success:(void (^)(id))successBlock failure:(void (^)(NSError *))failureBlock{
    
    [self POST:urlString parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        if (successBlock) {
            successBlock(responseObject);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (error) {
            failureBlock(error);
        }
    }];
    
    
}

//加载新鲜热卖产品数据
- (void)loadFreshProductsWithSuccessBlock:(void (^)())successBlock andFailureBlock:(void (^)(NSError *))failure {
    
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    
    [parameters setValue:@"2" forKey:@"call"];
    
    [[BQAccountManager sharedInstance] POSTWithUrlString:shop_url parameters:parameters success:^(id responseObject) {
        
        self.freshProducts = [NSArray yy_modelArrayWithClass:[BQProductModel class] json:responseObject[@"data"]];
        
        if (successBlock) {
            
            successBlock();
            
        }
        
    } failure:^(NSError *error) {
        
        if (failure) {
            failure(error);
        }
        
    }];
    
}

//加载超市产品数据
- (void)loadProductWithSuccessBlock:(void(^)()) successBlock andFailureBlock:(void(^)(NSError *)) failure {
    
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    
    [parameters setValue:@"5" forKey:@"call"];
    
    [[BQAccountManager sharedInstance] POSTWithUrlString:supermarket_url parameters:parameters success:^(id responseObject) {
        
        NSDictionary *dict = responseObject[@"data"];
        
        NSArray *dataArray = dict[@"categories"];
        
        NSDictionary *productDict = dict[@"products"];
        
        self.categories = [NSArray yy_modelArrayWithClass:[BQCategoryModel class] json:dataArray];
        
        for (BQCategoryModel *model in self.categories) {
            
            model.products = [NSArray yy_modelArrayWithClass:[BQProductModel class] json:productDict[model.id]];
            
        }
        
        [self mergeTheModel];
        
        if (successBlock) {
            
            successBlock();
            
        }
        
    } failure:^(NSError *error) {
        
        if (failure) {
            failure(error);
        }
        
    }];
    
}

- (void)loadSearchProductResultsWithSuccessBlock:(void(^)()) successBlock andFailureBlock:(void(^)(NSError *)) failure {
    
    NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];
    
    [dict setValue:@"8" forKey:@"call"];
    
    [[BQAccountManager sharedInstance] POSTWithUrlString:searchResult_url parameters:dict success:^(id responseObject) {
    
        self.searchResults = [NSArray yy_modelArrayWithClass:[BQProductModel class] json:responseObject[@"data"]];
        
        if (successBlock) {
            successBlock();
        }
        
    } failure:^(NSError *error) {
    
        if (failure) {
            failure(error);
        }
    
    }];
    
}

//合并相同的产品模型
- (void)mergeTheModel {
    
    for (BQProductModel *freshProduct in self.freshProducts) {
        
        for (BQCategoryModel *category in self.categories) {
            
            NSMutableArray *productArr = [NSMutableArray arrayWithCapacity:category.products.count];
            
            for (BQProductModel *product in category.products) {
                
                if ([product.id isEqualToString:freshProduct.id]) {
                    
                    [productArr addObject:freshProduct];
                    
                } else {
                    
                    [productArr addObject:product];
                    
                }
                
            }
            
            category.products = productArr;
            
        }
        
    }
    
}

@end
